Stored procedures provide database
engineers with a useful way of storing and recalling code when needed. Over the next few minutes, you'll receive a quick recap of MySQL
storage procedures and how they work. Let's begin with a look at how stored
procedures can help Little Lemon. Little Lemon checks the online bookings
in its database every morning for a list of customers attending
the restaurant that day. They rewrite the same code each
morning to extract this data. But they could instead invoke a stored
procedure that extracts the required data without the need to rewrite large
blocks of code every morning. Before you find out how they can do this, let's recap the basics
of stored procedures. A stored procedure is a block of code or
one or more pre-prepared queries that
can be stored in your database. You can then invoke or
call the stored procedure as required. As you might already know,
this is similar to how a function works. But don't forget the key difference
between the two concepts. Functions can only have input parameters,
but a stored procedure can have both input and
output parameters. There are three main steps to complete
when using stored procedures. You should already be familiar with these. First, you create a stored procedure,
then you call a stored procedure and finally, you can drop or
delete a stored procedure. Let's begin with a recap of
the benefits of stored procedures. Your code is more consistent. The same code block is used
each time it's invoked. You know exactly what to expect from it. Your code is reusable. You can use it as many times as you
need across all your database tasks. And your code is easier to use and
maintain. It's stored as one block that can be
invoked, edited or dropped as required. Next, let's quickly recap the syntax for
creating a stored procedure. To create a stored procedure,
begin with the CREATE PROCEDURE command. Then write the procedure name and a pair
of parenthesis to hold the parameters. Make sure that you include all required
parameters within the parenthesis. Finally, write the rest
of the procedure logic. Then when you need to
invoke the procedure, you just type the CALL command
followed by the procedure name. Don't forget to include the parenthesis. And if you need to remove or drop
a stored procedure from your database, then just type the DROP PROCEDURE
command followed by the procedure name. In this instance,
you don't need to include any parenthesis. Little Lemon can use this code to create a
stored procedure that extracts the details of the customers due to
visit the restaurant. They begin with
the CREATE PROCEDURE command. Then they name the procedure, daily_customer_details and
add the parameters. Finally, they write
the logic of the procedure. Now, each morning,
they just need to type the CALL command followed by daily_customer_details. The stored procedure then extracts the
required customer data from the database. So now that you've recapped
the concept of stored procedures, you might be asking yourself,
how do stored procedures relate to Python? Stored procedures increase the performance
of Python applications and reduce traffic between the application and
the MySQL database. The application only needs to send
the name of the stored procedure and parameter to the database instead
of a large block of SQL statements. You're now familiar with the benefits
of stored procedures and how to create, invoke and drop them in a database. You are now ready to learn how to perform
these actions using Python. Great work.